package com.innoking.docsys.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @Title:json转后台接收参数Util
 * @Description:
 * @author: sunq
 * @date: 2018\7\9 0009 11:58
 */
public class JsonToOtherUtil {

    /**
     * 将body字符参数转为参数实体对象
     * @param data
     * @param obj
     * @return
     */
    public static OtherPojo rtOtherPojo(String data,Object obj){
        JSONObject jsonObject=JSONObject.parseObject(data);
        OtherPojo otherPojo = new OtherPojo();
        otherPojo.setToken(jsonObject.getString("token"));
        otherPojo.setDeviceId(jsonObject.getString("deviceId"));
        if(obj!=null){
            String objStr = jsonObject.getString("obj");
            if(StringUtil.isNotEmpty(objStr)){
                Object cls=JSONObject.parseObject(objStr, obj.getClass());
                otherPojo.setObj(cls);
            }
        }
        otherPojo.setPage(jsonObject.getIntValue("page"));
        otherPojo.setMsg(jsonObject.getString("msg"));
        return otherPojo;
    }

    /**
     * 将参数对象封装成jsonstr提交get controller url post
     * @author sunq
     * @param obj
     * @return
     */
    public static String parseObj2RestParam( Object obj){

        OtherPojo ob=new OtherPojo();
        String jsonObj=JSONObject.toJSONString(obj);
        ob.setObj(jsonObj);
        String paramJson=JSONObject.toJSONString(ob);

        return paramJson;
    }

    /**
     * 将body字符参数转为参数String
     * @author wr
     * @param data
     * @return
     */
    public static OtherPojo rtOtherString(String data){
        JSONObject jsonObject=JSONObject.parseObject(data);
        OtherPojo otherPojo = new OtherPojo();
        otherPojo.setToken(jsonObject.getString("token"));
        otherPojo.setDeviceId(jsonObject.getString("deviceId"));
        String objStr = jsonObject.get("obj").toString();
        if(StringUtil.isNotEmpty(objStr)){
            otherPojo.setObj(objStr);
        }
        otherPojo.setPage(jsonObject.getIntValue("page"));
        otherPojo.setMsg(jsonObject.getString("msg"));
        return otherPojo;
    }

    /**
     * 封装OtherPojo
     * @param objMap 入参为obj中参数
     * @return
     */
    public static String packageEntering(Map<String,Object> objMap){
        OtherPojo otherPojo = new OtherPojo();
        otherPojo.setToken("1");
        otherPojo.setDeviceId("1");
        otherPojo.setPage(1);
        otherPojo.setMsg("");
        Object obj = JSON.toJSON(objMap);
        otherPojo.setObj(obj);
        String data = JSON.toJSONString(otherPojo);
        return data;
    }

    /**
     * 将result结果转为参数实体对象
     * @param data
     * @param obj
     * @return
     */
    public static ResultObjVO rtResultObjVO(String data,Object obj){
        JSONObject jsonObject=JSONObject.parseObject(data);
        ResultObjVO<Object> objectResultObjVO = new ResultObjVO<Object>();
        objectResultObjVO.setStatus(jsonObject.getString("status"));
        objectResultObjVO.setMsg(jsonObject.getString("msg"));
        if(obj != null){
            String objStr = jsonObject.getString("obj");
            if(StringUtil.isNotEmpty(objStr)){
                Object o = JSONObject.parseObject(objStr, obj.getClass());
                objectResultObjVO.setObj(o);
            }
        }
        return objectResultObjVO;
    }


    public static void main(String[] args) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("userId","1");
        String s = packageEntering(objectObjectHashMap);
        System.out.println(s);
    }


}
